package com.example.bodycontrolmodule.impl

import com.example.datacore.Event
import com.example.datacore.IBCMRepository
import com.example.datacore.IEventRepository
import com.example.sourceslibs.SignalType
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject
import javax.inject.Qualifier
import javax.inject.Singleton

/**
 * 限定符定义 用来区别同一个接口的不同实现
 * */
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class SkyWindowRepository



interface ISkyWindowRepository: IEventRepository {
    fun getSkyWindowPosFlow(): StateFlow<Int?>

    fun getSkyWindowStateFlow(): StateFlow<Int?>

    suspend fun setSkyWindowPos(pos:Int):Result<Boolean>
}


class SkyWindowRepositoryImpl @Inject constructor(
    private val bcmRepository: IBCMRepository
):ISkyWindowRepository{


    override fun getSkyWindowPosFlow(): StateFlow<Int?> {
        return bcmRepository.getStateFlow<Int?>(SignalType.CAR_SKY_WINDOW_POS)
    }

    override fun getSkyWindowStateFlow(): StateFlow<Int?> {
        return bcmRepository.getStateFlow<Int?>(SignalType.CAR_SKY_WINDOW_STATE)
    }

    override suspend fun setSkyWindowPos(pos: Int): Result<Boolean> {
        return bcmRepository.changeState(SignalType.CAR_SKY_WINDOW_POS,pos)
    }

    override fun getEventFlow(): SharedFlow<Event?> {
        TODO("Not yet implemented")
    }

}


@Module
@InstallIn(SingletonComponent::class)
abstract class SkyWindowRepositoryModule{
    @Binds
    @Singleton
    @SkyWindowRepository
    abstract fun createRepository(impl:SkyWindowRepositoryImpl):ISkyWindowRepository

}